package org.example.web;

import com.lysh.simple.FileUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.example.dto.WordTemplate;
import org.example.utils.PdfUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liyishan
 * @date 2024/3/12 13:56
 * @apiNote
 */
@RestController
@RequestMapping(value = "/test")
public class FileController {

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public void upload(@RequestPart("file") MultipartFile file) {
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public void test() {
        Map<String, String> params = new HashMap<>();
        params.put("signYear", "2024");
        params.put("signMonth", "03");
        params.put("signDay", "12");
        params.put("customerName", "xxx");
        params.put("customerLegalPerson", "法人");
        params.put("distributeCompanyName", "下发公司");
        //模版路径
        String templatePath = "/Users/liyishan/Desktop/cooperator_service_type_change_template.docx";
        //文件生成路径
        String wordFilePath = "/Users/liyishan/Desktop/picture/myFdf";
        String wordFileName = wordFilePath + ".docx";
        String pdfFileName = wordFilePath + ".pdf";
        File templateFile = new File(templatePath);
        //生成新文件
        File wordFile = new File(wordFileName);
        File pdfFile = null;
        try (InputStream is = Files.newInputStream(templateFile.toPath());
             OutputStream os = Files.newOutputStream(wordFile.toPath());
             BufferedOutputStream bos = new BufferedOutputStream(os)) {

            XWPFDocument docx = new XWPFDocument(is);
            WordTemplate wordTemplate = new WordTemplate(docx);
            wordTemplate.replaceTag(params);
            wordTemplate.replaceTables(params);
            XWPFTable table = docx.getTables().get(0);
            int rowSize = 3;
            for (int i = 1; i <= rowSize; i++) {
                table.createRow();
                XWPFTableRow row = table.getRow(i);
                row.getCell(0).setText(String.valueOf(i));
                row.getCell(1).setText("test" + i);
                row.getCell(1).setText("test2" + i);
            }
            os.flush();
            bos.flush();
            wordTemplate.write(os);
            PdfUtils.word2pdf(wordFileName, pdfFileName);
            pdfFile = new File(pdfFileName);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (wordFile != null && wordFile.exists()) {
                boolean delete = wordFile.delete();
            }
            System.out.println("删除临时文件");
        }

    }


    @RequestMapping(value = "/url2File", method = RequestMethod.GET)
    @ResponseBody
    public void url2File(String url) {
        try{
            int suffixIndex = url.lastIndexOf(".");
            String suffix = url.substring(suffixIndex);
            String wordFilePath = "/Users/liyishan/Desktop/picture/word11"+suffix;
            FileUtils.downloadFile(url, wordFilePath);
        }catch(Exception e){
            e.printStackTrace();
        }




    }

}
